// pages/collageDetail/collageDetail.js
var config = require('../util/config.js');
var $=require('../util/commit.js');
var imageArr=[];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param:{},
    showSubmitC:false,//显示提交信息状态
    DetailS:4,
    collageDage: {
      title: '',//拼团标题
      pic: '',//海报
      pic_id: '',//海报ID
      collage_id: '',//拼团分类ID
      collage_number: '', //人数ID
      register_end_time: '',//报名截止时间
      begin_time: '',//活动开始时间 
      end_time: '',//活动截止时间
      type: '',//活动类型 1线上 线下
      price_id: '',//报名费用ID
      address:'',//地址
      detailed_address:'',//详细地址
      address_x: '',//地址坐标 经度
      address_y: '2',//地址坐标 纬度
      content:[]
    },
    cuid:'',
    name:'',
    phone:'',
    expressNumber:'',
    expressCompany:'',
    expressAddress:'',
    input_bottom:0,
    view_height:603,
    screenWidth: 0,
    screenHeight: 0, 
    imageArrH:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          screenHeight: res.windowHeight,
          screenWidth: res.windowWidth,
        });
      }
    });
    let param=JSON.parse(options.data);
    that.setData({
      param,
    });

    that.loadData();
  },
   loadData:function(e){
   
     let that=this;
     $.POST({
       url:'caSCD',
       data:{
         caid:that.data.param.id,
       }
     },function(e){
       that.setData({
         collageDage:e.data
       })
     },function(e){})
  },
  imageLoad:function(e){
    let that=this;
    var width = '';
    var height = '';
    var viewHeight = '';    //获取图片真实宽度
    var screenWidth = that.data.screenWidth;
    var screenHeight = that.data.screenHeight;
    console.log()
    console.log()
    let url = e.currentTarget.dataset.url;
    url = url.substring(7);
    url = 'https://' + url;


    wx.getImageInfo({
      src: url,
      success: function (res) {
        width = res.width;
        height = res.height;
        let ratio = width / height;   //图片的真实宽高比例
        viewHeight = screenWidth / ratio * 2;    //计算的高度值   
        imageArr[e.currentTarget.dataset.index]=viewHeight;
       that.setData({
        imageArrH:imageArr,
       })
        // that.setData({
        //   imgwidth: res.width,
        //   imgheight: res.height,
         
        // })

        console.log(this)

      },
      fail: function (res) {

      }
    })



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // keyName
  keyName:function(e){
    let that=this;
    that.setData({
      name:e.detail.value,
    })
  },
  // keyPhone
  keyPhone:function(e){
    let that=this;
    that.setData({
      phone: e.detail.value,
    })

  },
  //keyExpressNumber
  keyExpressNumber:function(e){
    let that = this;   
    that.setData({
      expressNumber: e.detail.value,
    })
  },
  //keyExpressCompany
  keyExpressCompany: function (e) {
    let that = this;
    that.setData({
      expressCompany: e.detail.value,
    })
  },
  //keyExpressAddress
  keyExpressAddress: function (e) {
    let that = this;
    that.setData({
      expressAddress: e.detail.value,
    })
  },
  // 我要报名
  BMBtn:function(e){
    let that = this;
    
    if (that.data.collageDage.collage_id == '31') {
      this.setData({
        view_height: 867
      });
      this.setData({
        input_bottom: 117
      });
    }
    that.setData({

      showSubmitC: true,
    });
  
  },
  // 隐藏提交信息
  closeSC:function(){
    let that = this;
    that.setData({
      showSubmitC: false,
    });
  },
  // 确认
  sureBtn:function(e){
    let that =this;

    let title = e.currentTarget.dataset.title;
    let price = e.currentTarget.dataset.price;
    if(that.data.name){
      if(that.data.phone){
        var information = [
          { 'title': '姓名', 'type': 'text', 'value': that.data.name },
          { 'title': '手机号', 'type': 'text', 'value': that.data.phone },
        ];
        if (that.data.collageDage.collage_id == '31'){
          if (!that.data.expressNumber) {
            wx.showToast({
              title: '请填写取件码',
              icon: 'none'
            })
            return false;
          }
          if (!that.data.expressCompany) {
            wx.showToast({
              title: '请填写取件地址',
              icon: 'none'
            })
            return false;
          }
          if (!that.data.expressAddress) {
            wx.showToast({
              title: '请填写收件地址',
              icon: 'none'
            })
            return false;
          }
          
          var information_arr = [
            { 'title': '取件号', 'type': 'text', 'value': that.data.expressNumber },
            { 'title': '取件地址', 'type': 'text', 'value': that.data.expressCompany },
            { 'title': '收件地址', 'type': 'text', 'value': that.data.expressAddress },
          ];
          information = information.concat(information_arr);
        }
        console.log(information)
        $.POST({
          url: 'caSUC',
          data: {
            caid: e.currentTarget.dataset.id,
            name: that.data.name,
            mobile: that.data.phone,
            information: JSON.stringify(information)
          }
        }, function (e) {
          that.closeSC();
          if (e.data.status=="1"){
            // 未学生认证
            wx.showModal({
              title: '提示',
              content: '需要进行学生认证',
              confirmText: '进行认证',
              success(res) {
                //新增修改认证
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../applicationSkills/applicationSkills'　　// 页面 A
                  
                  })
                  // wx.navigateToMiniProgram({
                  //   //appId: config.Config.XXAPPId,
                  //   path: 'pages/home/home?Type=applicationSkills',
                  //   envVersion: 'release',
                  //   extraData: {
                  //     Type: 'applicationSkills'
                  //   },
                  //   success(res) {
                  //     // 打开成功
                  //   }
                  // })
                 
                } else if (res.cancel) {
                  console.log('用户点击取消')
                
                }
              }
            })
          }else{
            //跳转到支付页面
            $.openWin({
              url: '../payPage/payPage',
              data: {
                page: 'collageDetail',
                id: e.data.cuid,
                title: title,
                price: price,
              }
            })
          }
         
        }, function (e) {

        })
      }else{
        wx.showToast({
          title: '请填写手机号',
          icon: 'none'
        })
      }
    }else{
      wx.showToast({
        title: '请填写姓名',
        icon:'none'
      })
    }
  },
  // 取消报名
  cancleCollage:function(e){
    let that=this;
    wx.showModal({
      title: '提示',
      content: '确定要取消报名吗',
      success(res) {
        if (res.confirm) {
          $.POST({
            url: 'caCCSU',
            data: {
              cuid: e.currentTarget.dataset.id,
            }
          }, function (e) {
            wx.showToast({
              title: e.msg,
              icon: 'none',
            });
            that.loadData();
          }, function (e) { })
        } else if (res.cancel) {
          
        }
      }
    })
    
  },
  // HDgroup
  HDgroup:function(e){
    let that=this;
    wx.showToast({
      title: '功能搭建中',
    });
  },
  // 获取到焦点
  focus: function (e) {
    this.setData({
      input_bottom: 117
    })
  },

  // 失去焦点
  no_focus: function (e) {
    let that = this;
    if (that.data.collageDage.collage_id == '31') {
      this.setData({
        input_bottom: 117
      })
    } else {
      this.setData({
        input_bottom: 0
      })
    }
     
  },


})